package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.service.DynamicService;
import com.lonty.intelligence.vo.DynamicVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @Author 陈璇
 * @Date 2020/6/9 14:24
 * @Version 1.0
 */
@RestController
@RequestMapping("app/dynamic")
@Validated
@Api(tags = "动态接口", produces = "application/json")
public class DynamicController {

    @Resource
    private DynamicService dynamicService;

    @ApiOperation(value = "添加动态", httpMethod = "POST")
    @PostMapping("saveDynamic")
    public Result saveDynamic(DynamicVo dynamicVo){
        if(dynamicService.saveDynamic(dynamicVo)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "根据类型获取动态", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "动态类型(1:新闻 2:公告 3:活动 无:全部)",dataType = "int", paramType = "query")
    })
    @GetMapping("getDynamic")
    public Result getDynamic(Integer type){
        return Result.builder()
                .data(dynamicService.getDynamic(type))
                .build();
    }

    @ApiOperation(value = "根据ID获取动态", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "动态ID",dataType = "string", paramType = "query")
    })
    @GetMapping("getById")
    public Result getById(String id){
        return Result.builder()
                .data(dynamicService.getById(id))
                .build();
    }

    @ApiOperation(value = "删除动态", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "动态ID",dataType = "string", paramType = "query")
    })
    @PostMapping("deleteDynamic")
    public Result deleteDynamic(String id){
        if(dynamicService.deleteDynamic(id)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

}
